﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Essentials;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using static System.Net.Mime.MediaTypeNames;

namespace esome.SubPage
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class FileView : ContentView
    {
        public FileView()
        {
            InitializeComponent();
        }

        private async void File_Clicked(object sender, EventArgs e)
        {
            var customFileType =
                new FilePickerFileType(new Dictionary<DevicePlatform, IEnumerable<string>>
                {
                    { DevicePlatform.iOS, new[] { "public.my.comic.extension" } }, // or general UTType values
                    { DevicePlatform.Android, new[] { "application/comics" } },
                    { DevicePlatform.UWP, new[] { ".cbr", ".cbz" } },
                    { DevicePlatform.Tizen, new[] { "*/*" } },
                    { DevicePlatform.macOS, new[] { "cbr", "cbz" } }, // or general UTType values
                });
            var options = new PickOptions
            {
                PickerTitle = "Please select a text file",
                FileTypes = customFileType,
            };
            await PickAndShow(PickOptions.Default);
        }

        async Task<FileResult> PickAndShow(PickOptions options)
        {
            try
            {
                var result = await FilePicker.PickAsync(options);
                if (result != null)
                {
                    FileText.Text = $"File Name: {result.FullPath}";
                    SizeText.Text = "?";
                    if (result.FileName.EndsWith("jpg", StringComparison.OrdinalIgnoreCase) ||
                        result.FileName.EndsWith("png", StringComparison.OrdinalIgnoreCase))
                    {
                        var stream = await result.OpenReadAsync();
                        var Image = ImageSource.FromStream(() => stream);
                    }
                    else if (result.FileName.EndsWith("txt", StringComparison.OrdinalIgnoreCase) ||
                                result.FileName.EndsWith("log", StringComparison.OrdinalIgnoreCase) ||
                                result.FileName.EndsWith("dmlog", StringComparison.OrdinalIgnoreCase) ||
                                result.FileName.EndsWith("ini", StringComparison.OrdinalIgnoreCase) ||
                                result.FileName.EndsWith("cfg", StringComparison.OrdinalIgnoreCase) ||
                                result.FileName.EndsWith("config", StringComparison.OrdinalIgnoreCase) ||
                                result.FileName.EndsWith("cs", StringComparison.OrdinalIgnoreCase) ||
                                result.FileName.EndsWith("java", StringComparison.OrdinalIgnoreCase) ||
                                result.FileName.EndsWith("c", StringComparison.OrdinalIgnoreCase) ||
                                result.FileName.EndsWith("cpp", StringComparison.OrdinalIgnoreCase))
                    {
                        TextView.Text = "?";
                        //using var stream = await result.OpenReadAsync();
                        using var reader = new StreamReader(result.FullPath, true);
                        TextView.Text = await reader.ReadToEndAsync();
                        SizeText.Text = $"({reader.BaseStream.Length / 1024.0:0.00} KB)";
                    }
                }

                return result;
            }
            catch (Exception ex)
            {
                // The user canceled or something went wrong
                TextView.Text = ex.ToString();
            }

            return null;
        }
        async Task<double> GetStreamSizeAsync(FileResult result)
        {
            try
            {
                using var stream = await result.OpenReadAsync();
                return stream.Length / 1024.0;
            }
            catch
            {
                return 0.0;
            }
        }

    }
}